package com.atguigu.controller;

import com.atguigu.entity.Community;
import com.atguigu.entity.Dict;
import com.atguigu.service.CommunityService;
import com.atguigu.service.DictService;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/community")
public class CommunityController extends BaseController{

    @DubboReference //controller注入service用户DubboReference
    private CommunityService communityService;

    @DubboReference
    private DictService dictService;

    @RequestMapping
    public String findCommunity(HttpServletRequest request, Map map){
        Map<String, Object> filters = getFilters(request);
        PageInfo<Community> page = communityService.findPage(filters);
        //将filters和page放到map集合中做数据回显
        map.put("page",page);
        map.put("filters",filters);

        //查询北京下所有的区域
        List<Dict> areaList = dictService.findDictByParentCode("beijing");
        //将区域放到查到的区域放到请求域
        map.put("areaList",areaList);
        //跳转 到小区首页
        return "community/index";
    }

    //新增
    @RequestMapping("/create")
    public String create(Map map){
        //获取北京所有的区域
        List<Dict> areaList = dictService.findDictByParentCode("beijing");
        //将区域放到查到的区域放到请求域
        map.put("areaList",areaList);
        return "community/create";
    }

    //添加
    @RequestMapping("/save")
    public String save(Community community){
        communityService.insert(community);
        return "common/successPage";
    }

    /**
     * 查询
     * @param map 将查到的数据存储到作用域
     * @param communityId 查询到的小区id
     * @return 修改页面
     */
    @RequestMapping("/edit/{communityId}")
    public String edit(Map map,@PathVariable Long communityId){
        //获取北京所有的区域
        List<Dict> areaList = dictService.findDictByParentCode("beijing");
        //将区域放到查到的区域放到请求域
        map.put("areaList",areaList);
        Community community = communityService.getById(communityId);
        map.put("community",community);
        return "community/edit";
    }

    /**
     * 修改
     * @param community 小区信息
     * @return 修改成功页面
     */
    @RequestMapping("/update")
    public String update(Community community){
        communityService.update(community);
        return "common/successPage";
    }


    /**
     * 删除
     * @param communityId 小区id
     * @return 重定向到community页面
     */
    @RequestMapping("/delete/{communityId}")
    public String delete(@PathVariable Long communityId){
        communityService.delete(communityId);
        return "redirect:/community";
    }

}
